package javanetworking;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javanetworking.Server;
import javanetworking.host;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Admin
 */
public class Squery implements Serializable {

    private int code;
    private String Filename;
    private List<String> ListChunk;
    private int offset;
    private int PosInTracket;

    public Squery() {
    }

    
    
    public Squery(int codes,String filenams,List<String> lst) {
        this.code = codes;
        this.Filename = filenams;
        this.ListChunk = lst;
        this.offset = 0;
        this.PosInTracket = 0;
    }

    
//    public static boolean ktChunkTonTai(Squery sq) throws FileNotFoundException, IOException {
//        File fi_nodes = new File(sq.getFilename() + ".chunk");
//        if (fi_nodes.exists() == false) {
//            System.out.println("Khong tim thay chunk");
//            return false;
//        }
//        BufferedReader br = new BufferedReader(new FileReader(fi_nodes));
//        for (int i = 0; i <= sq.getiChunk(); i++) {
//            if (i == sq.getiChunk()) {
//                String[] line = br.readLine().split(" ");
//                File fi = new File("chunks/" + line[0]);
//                if (fi.exists() == true) {
//                    return true;
//                }
//
//            } else {
//                br.readLine();
//            }
//        }
//        return false;
//    }
//    public static void main(String[] args) throws FileNotFoundException, IOException
//    {
//        Squery sq=new Squery();
//        sq.setFilename("ccboot.7z");
//        sq.setiChunk(0);
//        
//        boolean kq=ktChunkTonTai(sq);
//        System.out.println(kq);
//    }

//    public Squery receiveSquery(Squery sq) throws FileNotFoundException, IOException {
//        Squery squery = new Squery();
//        switch (sq.code) {
//            case 0:
//                /*
//                 * Kiem ta neu co chunk ti set co =2
//                 */
//                if (ktChunkTonTai(sq) == true) {
//                    sq.setCode(2);
//                    return sq;
//                }
//                break;
//            case 1:
//                /*
//                 * 
//                 */
//
//                break;
//            case 2:
//                /*
//                 * Xac nhan yeu cau gui chunk set co=1
//                 */
//                sq.setCode(1);
//                break;
//            case 3:
//            /*
//             * 
//             */
//
//        }
//        return squery;
//    }

    public byte[] toByteArray() {
        ObjectOutputStream ois = null;
        ByteArrayOutputStream baos = null;
        byte[] bytes = null;
        try {
            baos = new ByteArrayOutputStream();
            ois = new ObjectOutputStream(baos);
            ois.writeObject(this);
            bytes = baos.toByteArray();
            baos.close();
            ois.close();
        } catch (Exception e) {
        }
        return bytes;
    }

    /**
     * @return the code
     */
    public int getCode() {
        return code;
    }

    /**
     * @param code the code to set
     */
    public void setCode(int code) {
        this.code = code;
    }

    /**
     * @return the Filename
     */
    public String getFilename() {
        return Filename;
    }

    /**
     * @param Filename the Filename to set
     */
    public void setFilename(String Filename) {
        this.Filename = Filename;
    }

    /**
     * @return the PosInTracket
     */
    public int getPosInTracket() {
        return PosInTracket;
    }

    /**
     * @param PosInTracket the PosInTracket to set
     */
    public void setPosInTracket(int PosInTracket) {
        this.PosInTracket = PosInTracket;
    }

    

    /**
     * @return the offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * @param offset the offset to set
     */
    public void setOffset(int offset) {
        this.offset = offset;
    }

    /**
     * @return the ListChunk
     */
    public List<String> getListChunk() {
        return ListChunk;
    }

    /**
     * @param ListChunk the ListChunk to set
     */
    public void setListChunk(List<String> ListChunk) {
        this.ListChunk = ListChunk;
    }
}
